<div class={computedClass} ref:node>
    {@html massagedContent}
</div>
<style>
  .status-content {
    margin: 10px 10px 10px 5px;
    grid-area: content;
    word-wrap: break-word;
    overflow: hidden;
    white-space: pre-wrap;
    font-size: 0.9em;
    display: none;
  }

  .status-content.status-in-own-thread {
    font-size: 1.3em;
    margin: 20px 10px 20px 5px;
  }

  .status-content.shown {
    display: block;
  }

  :global(.status-content li) {
    font-size: 1.3em;
  }

  :global(
      .status-content p,
      .status-content blockquote,
      .status-content ul,
      .status-content ol) {
    margin: 0 0 20px;
  }

  :global(
      .status-content p:first-child,
      .status-content blockquote:first-child,
      .status-content ul:first-child,
      .status-content ol:first-child) {
    margin: 0 0 20px;
  }

  :global(
      .status-content p:last-child,
      .status-content blockquote:last-child,
      .status-content ul:last-child,
      .status-content ol:last-child) {
    margin: 0;
  }

  :global(.status-content blockquote) {
    padding-left: 1.5rem;
    border-left: 5px solid var(--body-bg);
    color: var(--very-deemphasized-text-color);
    font-style: italic;
  }

  :global(.status-content ul, .status-content ol) {
    padding-left: 2rem;
  }

  .status-content.status-in-notification {
    color: var(--very-deemphasized-text-color);
  }
  :global(.status-content.status-in-notification a, .status-content.status-in-notification a:hover) {
    color: var(--very-deemphasized-link-color);
  }

  :global(.underline-links .status-content a) {
    text-decoration: underline;
  }

  @media (max-width: 240px) {
    :global(
      .status-content p:last-child,
      .status-content blockquote:last-child,
      .status-content ul:last-child,
      .status-content ol:last-child) {
      margin: 0 0 10px; /* looks better on KaiOS with some spacing here */
    }
  }

</style>
<script>
  import { mark, stop } from '../../_utils/marks.js'
  import { store } from '../../_store/store.js'
  import { classname } from '../../_utils/classname.js'
  import { massageUserText } from '../../_utils/massageUserText.js'
  import { urlIsCrossOrigin } from '../../_utils/urlIsCrossOrigin.js'

  export default {
    oncreate () {
      this.hydrateContent()
    },
    store: () => store,
    computed: {
      computedClass: ({ isStatusInOwnThread, isStatusInNotification, shown }) => {
        return classname(
          'status-content',
          isStatusInOwnThread && 'status-in-own-thread',
          isStatusInNotification && 'status-in-notification',
          shown && 'shown'
        )
      },
      content: ({ originalStatus }) => (originalStatus.content || ''),
      massagedContent: ({ content, originalStatusEmojis, $autoplayGifs }) => (
        massageUserText(content, originalStatusEmojis, $autoplayGifs)
      )
    },
    methods: {
      hydrateContent () {
        mark('hydrateContent')
        const node = this.refs.node
        const { originalStatus, uuid } = this.get()
        const { mentions, tags } = originalStatus
        let count = 0
        const anchors = Array.from(node.getElementsByTagName('A'))

        for (const anchor of anchors) {
          // hydrate hashtag
          if (tags && anchor.classList.contains('hashtag')) {
            for (const tag of tags) {
              if (anchor.getAttribute('href').toLowerCase().endsWith(`/${tag.name.toLowerCase()}`)) {
                anchor.setAttribute('href', `/tags/${tag.name}`)
                anchor.setAttribute('id', `status-content-link-${uuid}-${++count}`)
                anchor.removeAttribute('target')
                anchor.removeAttribute('rel')
              }
            }
          // hydrate mention
          } else if (mentions && anchor.classList.contains('mention')) {
            for (const mention of mentions) {
              if (anchor.getAttribute('href') === mention.url) {
                anchor.setAttribute('href', `/accounts/${mention.id}`)
                anchor.setAttribute('title', `@${mention.acct}`)
                anchor.setAttribute('id', `status-content-link-${uuid}-${++count}`)
                anchor.removeAttribute('target')
                anchor.removeAttribute('rel')
              }
            }
          }
          // hydrate external links
          const href = anchor.getAttribute('href')
          if (urlIsCrossOrigin(href)) {
            anchor.setAttribute('title', href)
            anchor.setAttribute('target', '_blank')
            anchor.setAttribute('rel', 'nofollow noopener')
          }
        }
        stop('hydrateContent')
      }
    }
  }
</script>
